package com.hls.mybatis.core;

import java.lang.annotation.Annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

import com.hls.mybatis.core.annotation.Mybatis;

public class BeanScannerConfigurer implements BeanDefinitionRegistryPostProcessor {
    
    private String basePackage;
    private Class<? extends Annotation> annotation = Mybatis.class;
    
    public void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    public void setAnnotation(Class<? extends Annotation> annotation) {
        this.annotation = annotation;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // TODO Auto-generated method stub
        GenericBeanDefinition myHandler = new GenericBeanDefinition();
        myHandler.setBeanClass(MyHandler.class);
        registry.registerBeanDefinition("myHandler", myHandler);
        BeanScanner scanner = new BeanScanner(registry, annotation);
        scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
    }

}
